package com.ilop.sthome.ui.fragment.scene;

import android.os.Bundle;

import com.example.common.base.DynamicFragmentAdapter;
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;

import android.view.View;

import com.example.common.base.BaseBindFragment;
import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.ui.activity.scene.AddSceneActivity;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.FragmentSceneBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * @author skygge
 * @date 2019-12-30.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：场景页面
 */
public class SceneFragment extends BaseBindFragment<FragmentSceneBinding> {
    private List<Fragment> mFragment;
    private List<String> mGatewayName;
    private DynamicFragmentAdapter mAdapter;
    private int mPosition = 0;

    private List<DeviceInfoBean> mGatewayList;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_scene;
    }

    @Override
    protected void initView() {
        super.initView();
        mFragment = new ArrayList<>();
        mGatewayName = new ArrayList<>();
    }

    @Override
    protected void initData() {
        super.initData();
        onCreateView();
        mAdapter = new DynamicFragmentAdapter(getChildFragmentManager(), mFragment);
        mDBind.sceneViewPager.setAdapter(mAdapter);
        mDBind.sceneTabLayout.setupWithViewPager(mDBind.sceneViewPager);
    }

    @Override
    protected void initDataBus() {
        super.initDataBus();
        LiveDataBus.get().with(CommonId.REFRESH_SCENE, Integer.class).observe(this, integer -> onRefreshView());
    }

    @Override
    protected void onFirstUserVisible() {
        super.onFirstUserVisible();
        LiveDataBus.get().with(CommonId.KEY_MODIFY_SCENE).setValue(0);
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.mainAddScene.setOnClickListener(view -> {
            Bundle bundle = new Bundle();
            bundle.putInt(CommonId.KEY_POSITION, mPosition);
            skipAnotherActivity(bundle, AddSceneActivity.class);
        });
        mDBind.sceneTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mPosition = tab.getPosition();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mDBind.sceneViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mDBind.sceneTabLayout.getTabLayout()));
    }

    /**
     * 创建
     */
    private void onCreateView(){
        mGatewayList = DeviceDaoUtil.getInstance().findAllGateway();
        if (mGatewayList!=null && mGatewayList.size() > 0){
            mDBind.mainAddScene.setVisibility(View.VISIBLE);
            mDBind.sceneWithout.setVisibility(View.GONE);
            for (DeviceInfoBean gateway:mGatewayList) {
                String nickname = gateway.getNickName() == null ? getString(R.string.ali_gateway) : gateway.getNickName();
                mGatewayName.add(nickname);
                mDBind.sceneTabLayout.addTab(nickname);
                mFragment.add(SceneChildFragment.newInstance(gateway.getDeviceName()));
            }
        }else {
            mDBind.mainAddScene.setVisibility(View.GONE);
            mDBind.sceneWithout.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 刷新
     */
    private void onRefreshView(){
        List<DeviceInfoBean> gatewayList = DeviceDaoUtil.getInstance().findAllGateway();
        if (gatewayList.size() > 0) {
            mDBind.mainAddScene.setVisibility(View.VISIBLE);
            mDBind.sceneWithout.setVisibility(View.GONE);
            if (!gatewayList.equals(mGatewayList)){
                refreshGateway(gatewayList);
                mGatewayList = gatewayList;
            }else {
                refreshGatewayName(gatewayList);
            }
        } else {
            mAdapter.updateData(new ArrayList<>());
            mDBind.mainAddScene.setVisibility(View.GONE);
            mDBind.sceneWithout.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 刷新网关场景
     */
    private void refreshGateway(List<DeviceInfoBean> mList){
        mFragment.clear();
        mGatewayName.clear();
        mDBind.sceneTabLayout.removeTab();
        for (DeviceInfoBean gateway:mList) {
            String nickname = gateway.getNickName() == null ? getString(R.string.ali_gateway) : gateway.getNickName();
            mGatewayName.add(nickname);
            mDBind.sceneTabLayout.addTab(nickname);
            mFragment.add(SceneChildFragment.newInstance(gateway.getDeviceName()));
        }
        mAdapter.updateData(mFragment);
        mDBind.sceneViewPager.setCurrentItem(mPosition);
    }

    /**
     * 刷新网关名称
     */
    private void refreshGatewayName(List<DeviceInfoBean> mList){
        for (int i = 0; i < mList.size(); i++) {
            DeviceInfoBean gateway = mList.get(i);
            String nickname = gateway.getNickName() == null ? getString(R.string.ali_gateway) : gateway.getNickName();
            String oldName = mGatewayName.get(i);
            if (!nickname.equals(oldName)){
                mDBind.sceneTabLayout.modifyTabName(i, nickname);
            }
        }
    }
}
